﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Media.Animation;
using PairingMatrix.Common.Enum;
using PairingMatrix.ViewModel;
using System.Linq;

namespace PairingMatrix.View.Behavior
{
    public class MoveTeamMemberCardToShowPairingMatrixAndIceCreamMeterBehavior : Behavior<TeamMemberCardControl>
    {
        private ContentPresenter _parent;
        private TeamMemberCardViewModel _teamMemberCardViewModel;
        private Storyboard _storyboardMove;
        private DoubleAnimation _xAnimation;
        private DoubleAnimation _yAnimation;
        private Storyboard _storyboardMoveEasy;
        private DoubleAnimation _xAnimationEasy;
        private DoubleAnimation _yAnimationEasy;
        private Storyboard _storyboardOpacity;
        private DoubleAnimation _opacityAnimation;

        protected override void OnAttached()
        {
            AssociatedObject.Loaded += (sender, e) =>
            {
                _teamMemberCardViewModel = (TeamMemberCardViewModel)AssociatedObject.DataContext;
                _teamMemberCardViewModel.PropertyChanged += TeamMemberCard_PropertyChanged;

                if (_parent == null)
                {
                    _parent = (ContentPresenter)VisualTreeHelper.GetParent(AssociatedObject);
                }

                var duration = new Duration(TimeSpan.FromSeconds(2));

                _xAnimation = new DoubleAnimation();
                _yAnimation = new DoubleAnimation();

                _xAnimation.Duration = duration;
                _yAnimation.Duration = duration;
                
                _xAnimation.EasingFunction = new ElasticEase() { Oscillations = 2, Springiness = 6, EasingMode = EasingMode.EaseInOut };
                _yAnimation.EasingFunction = new ElasticEase() { Oscillations = 2, Springiness = 6, EasingMode = EasingMode.EaseInOut };
                
                _storyboardMove = new Storyboard();
                _storyboardMove.Duration = duration;

                _storyboardMove.Children.Add(_xAnimation);
                _storyboardMove.Children.Add(_yAnimation);

                Storyboard.SetTarget(_xAnimation, _parent);
                Storyboard.SetTarget(_yAnimation, _parent);

                Storyboard.SetTargetProperty(_xAnimation, new PropertyPath("(Canvas.Left)"));
                Storyboard.SetTargetProperty(_yAnimation, new PropertyPath("(Canvas.Top)"));

            
                //animation for pairwith controls
                _opacityAnimation = new DoubleAnimation();
                _opacityAnimation.Duration = TimeSpan.FromSeconds(1);
                _storyboardOpacity = new Storyboard();
                _storyboardOpacity.Duration = TimeSpan.FromSeconds(1);
                _storyboardOpacity.Children.Add(_opacityAnimation);
                Storyboard.SetTarget(_opacityAnimation, AssociatedObject.PairedWithCollection);
                Storyboard.SetTargetProperty(_opacityAnimation, new PropertyPath("Opacity"));


                //easy move animation for card
                var durationEasy = new Duration(TimeSpan.FromSeconds(1));
                _xAnimationEasy = new DoubleAnimation();
                _yAnimationEasy = new DoubleAnimation();

                _xAnimationEasy.Duration = durationEasy;
                _yAnimationEasy.Duration = durationEasy;

                _xAnimationEasy.EasingFunction = new PowerEase() {Power = 5, EasingMode = EasingMode.EaseOut };
                _yAnimationEasy.EasingFunction = new PowerEase() {Power = 5, EasingMode = EasingMode.EaseOut };

                _storyboardMoveEasy = new Storyboard();
                _storyboardMoveEasy.Duration = durationEasy;

                _storyboardMoveEasy.Children.Add(_xAnimationEasy);
                _storyboardMoveEasy.Children.Add(_yAnimationEasy);

                Storyboard.SetTarget(_xAnimationEasy, _parent);
                Storyboard.SetTarget(_yAnimationEasy, _parent);

                Storyboard.SetTargetProperty(_xAnimationEasy, new PropertyPath("(Canvas.Left)"));
                Storyboard.SetTargetProperty(_yAnimationEasy, new PropertyPath("(Canvas.Top)"));

                _storyboardMoveEasy.Completed += _storyboardEasy_Completed;
                _storyboardOpacity.Completed += _storyboardOpacity_Completed;

            };

            AssociatedObject.MouseLeftButtonDown += (sender, e) =>
                                                    {
                                                        if(_teamMemberCardViewModel.ArrangeCardToShowStatistics)
                                                        {
                                                            ((ICardWallViewModel)
                                                             ControlRegistry.CardWallControl.DataContext).TeamMembers.
                                                                ToList().ForEach(t => t.ShowStatistics = false);

                                                            _teamMemberCardViewModel.ShowStatistics = true;

                                                        }

                                                        if(_teamMemberCardViewModel.ArrangeCardToShowIceCreamMeter)
                                                        {
//                                                            AnimateCardToScaleDownAndUp();
                                                            _teamMemberCardViewModel.AddIceCream(DateTime.Today);
                                                        }
                                                    };
        }

        private void AnimateCardToScaleDownAndUp()
        {
            var duration = new Duration(TimeSpan.FromSeconds(0.5));

            var scaleAnimation = new DoubleAnimation();

            scaleAnimation.Duration = duration;

            var storyboardScale = new Storyboard();
            storyboardScale.Duration = duration;

            storyboardScale.Children.Add(scaleAnimation);

            Storyboard.SetTarget(scaleAnimation, AssociatedObject.rectTransform);

            Storyboard.SetTargetProperty(scaleAnimation, new PropertyPath("ScaleX"));

            scaleAnimation.To = 0.75;
            storyboardScale.Begin();
            scaleAnimation.To = 1;
            storyboardScale.Begin();


        }


        void TeamMemberCard_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ArrangeCardToShowStatistics")
            {

                if (_teamMemberCardViewModel.ArrangeCardToShowStatistics)
                {
                    MoveCardFast(115 * _teamMemberCardViewModel.Order - 100, 110);
                }
                else
                {
                    AssociatedObject.PairedWithCollection.Visibility = Visibility.Collapsed;
                    AssociatedObject.PairedWithCollection.Opacity = 0;
                    MoveCardFast(_teamMemberCardViewModel.Position.X, _teamMemberCardViewModel.Position.Y);
                }
                
            }

            else  if (e.PropertyName == "ShowStatistics")
            {

                if (_teamMemberCardViewModel.ShowStatistics)
                {
                    MoveCardEasily(400, 500);
                }
                else
                {
                    AssociatedObject.PairedWithCollection.Visibility = Visibility.Collapsed;
                    _opacityAnimation.To = 0.0;
                    _storyboardOpacity.Begin();
                }
                
            }
        
            else if (e.PropertyName == "ArrangeCardToShowIceCreamMeter")
            {

                if (_teamMemberCardViewModel.ArrangeCardToShowIceCreamMeter)
                {
                    MoveCardFast(300, 115 * _teamMemberCardViewModel.Order - 50);
                }
                else
                {
                    MoveCardFast(_teamMemberCardViewModel.Position.X, _teamMemberCardViewModel.Position.Y);
                }

            }

        }


        private void MoveCardFast(double x, double y)
        {
            _xAnimation.To = x ;
            _yAnimation.To = y;
            _storyboardMove.Begin();
        }


        private void MoveCardEasily(double x, double y)
        {
            _xAnimationEasy.To = x ;
            _yAnimationEasy.To = y;
            _storyboardMoveEasy.Begin();
        }

        void _storyboardEasy_Completed(object sender, EventArgs e)
        {
            if (_teamMemberCardViewModel.ShowStatistics)
            {
                AssociatedObject.PairedWithCollection.Visibility = Visibility.Visible;
                _opacityAnimation.To = 1.0;
                _storyboardOpacity.Begin();
            }
        }

        void _storyboardOpacity_Completed(object sender, EventArgs e)
        {
            if (!_teamMemberCardViewModel.ShowStatistics)
            {
                MoveCardEasily(115*_teamMemberCardViewModel.Order - 100, 110);
            }
        }

    }
}
